package com.freshcity.manager.support
{
	import com.freshcity.component.container.Window;
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;

	public class WindowLayer extends UILayer
	{
		private var dcPopingWindow:Dictionary;

		public function WindowLayer()
		{
			this.dcPopingWindow=new Dictionary(true);
		}

		public override function addPopUp(arg1:DisplayObject, arg2:Boolean=false):void
		{
			var displayObject:DisplayObject;
			var modal:Boolean=false;
			var tl:TweenLite;

			var loc1:*;
			tl=null;
			displayObject=arg1;
			if (displayObject && (this.contains(displayObject) == false || this.isPoping(displayObject)))
			{
				this.addChild(displayObject);

				displayObject.alpha=0;
				tl=new TweenLite(displayObject, 0.5, {"alpha": 1, "ease": Cubic.easeOut, "onComplete": function():void
				{
					removePoping(displayObject);
					return;
				}})
				this.addPoping(displayObject, tl);

			}
			return;
		}

		public override function removePopup(arg1:DisplayObject):void
		{
			var displayObject:DisplayObject;
			var tl:TweenLite;

			var loc1:*;
			tl=null;
			displayObject=arg1;
			if (this.contains(displayObject))
			{
				displayObject.alpha=1;
				tl=new TweenLite(displayObject, 0.5, {"alpha": 0, "ease": Cubic.easeOut, "onComplete": function():void
				{
					removePoping(displayObject);
					removeChild(displayObject);
					return;
				}})
				this.addPoping(displayObject, tl);
			}
			return;
		}

		internal function addPoping(arg1:Object, arg2:TweenLite):void
		{
			this.removePoping(arg1);
			this.dcPopingWindow[arg1]=arg2;
			arg2.play();
			return;
		}

		internal function isPoping(arg1:Object):Boolean
		{
			var loc1:*=this.dcPopingWindow[arg1];
			return !(loc1 == null);
		}

		internal function removePoping(arg1:Object):void
		{
			var loc1:*=null;
			if (this.isPoping(arg1))
			{
				loc1=this.dcPopingWindow[arg1];
				loc1.pause();
				loc1.kill();
				loc1=null;
				delete this.dcPopingWindow[arg1];
			}
			return;
		}

		public override function centerPopup(arg1:DisplayObject):void
		{
			arg1.x=(stage.stageWidth - arg1.width) / 2;
			arg1.y=(stage.stageHeight - arg1.height) / 2;
			if (stage.stageHeight - arg1.height - 100 < arg1.y)
			{
				arg1.y=stage.stageHeight - arg1.height - 100;
			}
			if (stage.stageWidth - arg1.width - arg1.x - 280 < 0)
			{
				arg1.x=stage.stageWidth - arg1.width - 280;
			}
			if (arg1.x < 0)
			{
				arg1.x=0;
			}
			if (arg1.y < 0)
			{
				arg1.y=0;
			}
			return;
		}

		public function leftToChat(arg1:DisplayObject):void
		{
			if (!this.contains(arg1))
			{
				return;
			}
			arg1.x=280;
			return;
		}

		public function rightToTask(arg1:DisplayObject):void
		{
			if (!this.contains(arg1))
			{
				return;
			}
			arg1.x=stage.stageWidth - arg1.width - 280;
			return;
		}

		public function centerWindows(... rest):void
		{
			var loc1:*=0;
			var loc2:*=new Vector.<Window>();
			var loc3:*=10000;
			var loc4:*=0;
			while (loc4 < rest.length)
			{
				if (!(!rest[loc4] is Window))
				{
					loc2.push(rest[loc4] as Window);
					loc1=loc1 + (rest[loc4] as Window).width;
					this.centerPopup(rest[loc4] as Window);
					loc3=Math.min(loc3, (rest[loc4] as Window).y);
				}
				++loc4;
			}
			loc1=loc1 + (loc2.length - 1) * 5;
			var loc5:*=(stage.stageWidth - loc1) / 2;
			var loc6:*=0;
			while (loc6 < loc2.length)
			{
				if (loc2[loc6].isHide)
				{
					loc2[loc6].show();
				}
				this.setTop(loc2[loc6]);
				loc2[loc6].x=loc5;
				loc2[loc6].y=loc3;
				loc5=loc5 + (loc2[loc6].width + 5);
				++loc6;
			}
			return;
		}

		public function tweenCenterWindow(... rest):void
		{
			var loc7:*=NaN;
			var loc1:*=0;
			var loc2:*=new Vector.<Window>();
			var loc3:*=10000;
			var loc4:*=0;
			while (loc4 < rest.length)
			{
				if (!(!rest[loc4] is Window))
				{
					loc2.push(rest[loc4] as Window);
					loc1=loc1 + (rest[loc4] as Window).width;
					this.centerPopup(rest[loc4] as Window);
					loc3=Math.min(loc3, (rest[loc4] as Window).y);
				}
				++loc4;
			}
			loc1=loc1 + (loc2.length - 1) * 5;
			var loc5:*=(stage.stageWidth - loc1) / 2;
			var loc6:*=0;
			while (loc6 < loc2.length)
			{
				if (loc2[loc6].isHide)
				{
					loc2[loc6].show();
				}
				this.setTop(loc2[loc6]);
				loc7=(stage.stageWidth - loc2[loc6].width) / 2 >= loc5 ? loc5 + loc2[loc6].width / 3 : loc5 - loc2[loc6].width / 3;
				this.tweenWindow(loc2[loc6], loc7, loc3, loc5, loc3, 0.3);
				loc5=loc5 + (loc2[loc6].width + 5);
				++loc6;
			}
			return;
		}

		
		public function tweenWindow(win:Window, posX:Number, posY:Number, aimX:Number, aimY:Number, alphe:Number=0.7):void
		{
			win.x=posX;
			win.y=posY;
			TweenLite.to(win, alphe, {"x": aimX, "y": aimY});
		}
		

	}
}
